package com.linkme2night.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.linkme2night.R;
import com.linkme2night.controller.InteractWithVenueController;
import com.linkme2night.model.Comment;

public class IwvCommentsListAdapter extends ArrayAdapter<Comment> {

	private TextView commentBallon, commentDate;
	private List<Comment> commentsList = new ArrayList<Comment>();
	private LinearLayout wrapper;
	private long timeInMillis;
	private int seconds, minutes, hours;
	private int whoPosted;
	private InteractWithVenueController controller;

	public void add(Comment comment, int clubberID) {
		super.add(comment);
		commentsList.add(comment);
		this.whoPosted = clubberID;

	}

	public void addAll(List<Comment> list, int clubberID) {
		commentsList.addAll(list);
		this.whoPosted = clubberID;

	}

	public IwvCommentsListAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
	}

	public int getCount() {
		return this.commentsList.size();
	}

	public Comment getItem(int index) {
		return this.commentsList.get(index);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			LayoutInflater inflater = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.listitem_comments, parent, false);
		}

		wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

		Comment comment = getItem(position);

		commentBallon = (TextView) row.findViewById(R.id.comment);
		commentDate = (TextView) row.findViewById(R.id.textView_commentDate);
		controller = new InteractWithVenueController();
		commentBallon.setText(Html.fromHtml("<font color='blue'><b>"
				+ controller.getClubberName(comment.getClubberID())
				+ "</b></font>" + " " + comment.getText()));

		timeInMillis = comment.getDate().getTime();

		hours = (int) ((timeInMillis / (1000 * 60 * 60)) % 24);
		minutes = (int) ((timeInMillis / (1000 * 60)) % 60);
		seconds = (int) (timeInMillis / 1000) % 60;

		commentDate.setText(Integer.toString(hours) + ":"
				+ Integer.toString(minutes) + ":" + Integer.toString(seconds));


		// ballon collors
		commentBallon
				.setBackgroundResource((comment.getClubberID() == whoPosted) ? R.drawable.bubble_green
						: R.drawable.bubble_yellow);
		wrapper.setGravity((comment.getClubberID() == whoPosted) ? Gravity.RIGHT
				: Gravity.LEFT);

		return row;
	}

	public Bitmap decodeToBitmap(byte[] decodedByte) {
		return BitmapFactory
				.decodeByteArray(decodedByte, 0, decodedByte.length);
	}

	public void update(List<Comment> list) {
		commentsList.addAll(list);
	}

	public void refresh() {
		notifyDataSetChanged();		
	}

}